package org.pageley.games.sanctuary.view;

import java.util.Collection;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.pageley.games.sanctuary.domain.entity.creature.Creature;
import org.pageley.games.sanctuary.domain.environment.Environment;
import org.pageley.games.sanctuary.domain.event.Event;
import org.pageley.games.sanctuary.view.command.AttackCommand;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractCommandController;

public class AttackController extends AbstractCommandController {

	private Creature player;
	private Environment environment;
	
	public AttackController() {
		setCommandClass(AttackCommand.class);
	}
	
	public void setPlayer(Creature player) {
		this.player = player;
	}

	public void setEnvironment(Environment environment) {
		this.environment = environment;
	}
	
	@Override
	protected ModelAndView handle(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException exception)
			throws Exception {
		AttackCommand attackCommand = (AttackCommand) command;
		UUID id = UUID.fromString(attackCommand.getTargetId());
		Creature target = (Creature) environment.getEntity(id);
		this.player.attack(target);
		Collection<Event> events = environment.dequeueEvents();
        ModelAndView mav = new ModelAndView("update");
        mav.addObject("events", events);
		return mav;
	}

}
